package com.pri.chat.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.FriendsListBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.activity.OtherPeopleMsgActivity;
import com.pri.chat.adapter.FriendsAdapter;
import com.pri.chat.model.GzModel;
import com.pri.chat.model.UserModel;
import com.pri.chat.utils.Des3Util;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.fragment.FragmentLazy;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/24
 */
public class LiuShuiQingDanFragment extends FragmentLazy implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {

    Unbinder unbinder;
    @BindView(R.id.toolbar_search)
    EditText toolbarSearch;
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    private List<FriendsListBean> mList;
    private List<FriendsListBean> mShowList=new ArrayList<>();
    private FriendsAdapter mAdapter;
    private int type;
    private int page = 1;
    private boolean isShowDialog = false;

    public static LiuShuiQingDanFragment getInstance(int type) {
        LiuShuiQingDanFragment fragment = new LiuShuiQingDanFragment();
        fragment.type = type;
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        unbinder.unbind();
    }

    @Override
    protected View initViews(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = layoutInflater.inflate(R.layout.fragment_liushui_list_layout, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        initLayout();
        initSrl();
        return view;
    }

    private void initLayout() {
        mList = new ArrayList<>();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recMain.setLayoutManager(linearLayoutManager);
        mAdapter = new FriendsAdapter(R.layout.item_friends, mList,type);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setOnItemClickListener(this);
        toolbarSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mList.clear();
                for(int i=0;i<mShowList.size();i++){
                    if(mShowList.get(i).getNcName().contains(toolbarSearch.getText().toString())){
                        mList.add(mShowList.get(i));
                    }
                }
                if(mList.isEmpty()){
                    none.setVisibility(View.VISIBLE);
                }else {
                    none.setVisibility(View.INVISIBLE);
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<FriendsListBean>>> onNextListener = baseBean -> {
            if (baseBean.getData().isEmpty() && page == 1) {
                none.setVisibility(View.VISIBLE);
                mAdapter.notifyDataSetChanged();
                RxToast.normal(getString(R.string.no_data));
            } else {
                none.setVisibility(View.INVISIBLE);
                mShowList.addAll(baseBean.getData());

                if(toolbarSearch.getText().toString().isEmpty()){
                    mList.addAll(baseBean.getData());
                }else {
                    for(int i=0;i<mShowList.size();i++){
                        if(mShowList.get(i).getNcName().contains(toolbarSearch.getText().toString())){
                            mList.add(baseBean.getData().get(i));
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        };
        UserModel dtModel = new UserModel();//登录上传参数
        dtModel.setMemberId(SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        switch (type){
            case 0:
                HttpMethods.getInstance().getMyFenSi(new ProgressSubscriber<>(onNextListener, mContext, isShowDialog), requestBody);
                break;
            case 1:
                HttpMethods.getInstance().getGuanZhuMember(new ProgressSubscriber<>(onNextListener, mContext, isShowDialog), requestBody);
                break;
            case 2:
                HttpMethods.getInstance().getMyFriend(new ProgressSubscriber<>(onNextListener, mContext, isShowDialog), requestBody);
                break;
        }

    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
//        srlAll.setOnLoadmoreListener(refresh_layout -> {
//            refresh_layout.finishLoadmore(1000);
//            isShowDialog = false;
//            page++;
//            initData();
//        });
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        initData();
    }
    private void httpGz(int pos) {
        SubscriberOnNextListener<BaseBean> onNextListener = baseBean -> {
            RxToast.normal(baseBean.getMsg());
           srlAll.autoRefresh();
        };
        GzModel userModel = new GzModel();
        userModel.setGzrId(SharedHelper.readId(mContext));
        userModel.setBgzId(mList.get(pos).getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(userModel));
        HttpMethods.getInstance().saveGuanZhu(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()){
            case R.id.tv_friends_status:
                httpGz(position);
                break;
        }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//            NimUIKit.startP2PSession(mContext, mList.get(position).getId());
        Bundle bundle =new Bundle();
        bundle.putString("id",mList.get(position).getId());
        RxActivityTool.skipActivity(mContext, OtherPeopleMsgActivity.class,bundle);
    }
}
